/*
 *  Project     : QKeyboardStatistic
 *  Author      : Durachenko Aleksey V. <durachenko.aleksey@gmail.com>, 2011
 *  Licence     : GNU GENERAL PUBLIC LICENSE Version 2
 */
#include "cdatabase.h"

// ----------------------------------------------------------------------
CDatabase::CDatabase(QObject* parent) : QObject(parent)
{
}


// ----------------------------------------------------------------------
bool CDatabase::open(const QString& fileName)
{
    if (!QFileInfo(fileName).exists())
    {
        QFile::copy(":/db/default.db", fileName);
        QFile::setPermissions(fileName, QFile::ReadOwner|QFile::WriteOwner);
    }

    mDb = QSqlDatabase::addDatabase("QSQLITE");
    mDb.setDatabaseName(fileName);

    return mDb.open();
}


// ----------------------------------------------------------------------
bool CDatabase::isOpen() const
{
    return mDb.isOpen();
}


// ----------------------------------------------------------------------
bool CDatabase::writeTrio(const QDateTime& dateTime, qint64 key, qint64 click, qint64 mileage)
{
    QSqlQuery query(mDb);
    return query.exec(QString("INSERT INTO tstatistic(tstamp, keycount, clickcount, mileage) VALUES(%1, %2, %3, %4);")
                      .arg(dateTime.toTime_t()).arg(key).arg(click).arg(mileage));
}


// ----------------------------------------------------------------------
bool CDatabase::getTotalStatistic(qint64& key, qint64& click, qint64& mileage)
{
    QString queryString = "select sum(keycount), sum(clickcount), sum(mileage) from tstatistic;";
    QSqlQuery query(queryString, mDb);
    if (query.next())
    {
        key = query.value(0).toLongLong();
        click = query.value(1).toLongLong();
        mileage = query.value(2).toLongLong();
    }
    else
    {
        key = click = mileage = 0;
    }

    return true;
}


// ----------------------------------------------------------------------
bool CDatabase::getMonthStatistic(const QDateTime& dateTime, qint64& key, qint64& click, qint64& mileage)
{
    QDateTime dt = QDateTime(QDate(dateTime.date().year(), dateTime.date().month(), 1), QTime(0, 0, 0), Qt::LocalTime);
    QString queryString = QString("select sum(keycount), sum(clickcount), sum(mileage) from tstatistic where tstamp >= %1;").arg(dt.toTime_t());
    QSqlQuery query(queryString, mDb);
    if (query.next())
    {
        key = query.value(0).toLongLong();
        click = query.value(1).toLongLong();
        mileage = query.value(2).toLongLong();
    }
    else
    {
        key = click = mileage = 0;
    }

    return true;
}


// ----------------------------------------------------------------------
bool CDatabase::getWeekStatistic(const QDateTime& dateTime, qint64& key, qint64& click, qint64& mileage)
{
    QDateTime dt = QDateTime(QDate(dateTime.date().year(), dateTime.date().month(), dateTime.date().day()).addDays(-(dateTime.date().dayOfWeek()-1)), QTime(0, 0, 0), Qt::LocalTime);
    QString queryString = QString("select sum(keycount), sum(clickcount), sum(mileage) from tstatistic where tstamp >= %1;").arg(dt.toTime_t());
    QSqlQuery query(queryString, mDb);
    if (query.next())
    {
        key = query.value(0).toLongLong();
        click = query.value(1).toLongLong();
        mileage = query.value(2).toLongLong();
    }
    else
    {
        key = click = mileage = 0;
    }

    return true;

}


// ----------------------------------------------------------------------
bool CDatabase::getDayStatistic(const QDateTime& dateTime, qint64& key, qint64& click, qint64& mileage)
{
    QDateTime dt = QDateTime(QDate(dateTime.date().year(), dateTime.date().month(), dateTime.date().day()), QTime(0, 0, 0), Qt::LocalTime);
    QString queryString = QString("select sum(keycount), sum(clickcount), sum(mileage) from tstatistic where tstamp >= %1;").arg(dt.toTime_t());
    QSqlQuery query(queryString, mDb);
    if (query.next())
    {
        key = query.value(0).toLongLong();
        click = query.value(1).toLongLong();
        mileage = query.value(2).toLongLong();
    }
    else
    {
        key = click = mileage = 0;
    }

    return true;
}


// ----------------------------------------------------------------------
bool CDatabase::getHourStatistic(const QDateTime& dateTime, qint64& key, qint64& click, qint64& mileage)
{
    QDateTime dt = QDateTime(QDate(dateTime.date().year(), dateTime.date().month(), dateTime.date().day()), QTime(dateTime.time().hour(), 0, 0), Qt::LocalTime);
    QString queryString = QString("select sum(keycount), sum(clickcount), sum(mileage) from tstatistic where tstamp >= %1;").arg(dt.toTime_t());
    QSqlQuery query(queryString, mDb);
    if (query.next())
    {
        key = query.value(0).toLongLong();
        click = query.value(1).toLongLong();
        mileage = query.value(2).toLongLong();
    }
    else
    {
        key = click = mileage = 0;
    }

    return true;
}
